import 'package:flutter/material.dart';
import 'package:flutter_base/statemanagement_demo/models/goods_entity.dart';

class CartModel extends ChangeNotifier {
  final List<GoodsEntity> _items = [];

  double? get totalPrice {
    double totalPrice = 0.0;
    for (var item in _items) {
      totalPrice += item.price;
    }

    return totalPrice;
  }

  List<GoodsEntity> get items => _items;

  void add(GoodsEntity goodsEntity) {
    if (!isInCart(goodsEntity)) {
      _items.add(goodsEntity);

      notifyListeners();
    }
  }

  bool isInCart(GoodsEntity goodsEntity) {
    return _items.any((element) => element.id == goodsEntity.id);
  }

  void removeAll() {
    if (_items.isNotEmpty) {
      _items.clear();

      notifyListeners();
    }
  }
}
